﻿using WRJClient.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WRJClient.Common
{
    public sealed class WorkStationManager : DataManager<WorkStationViewModel>
    {
        private static readonly WorkStationManager _instance = new WorkStationManager();
        public static WorkStationManager Instance { get { return _instance; } }
        public int Count { get { return _datas.Count(); } }
        public int OnlineCount
        {
            get
            {
                return _datas.Count(m => m.IsOnline == true);
            }
        }
        private WorkStationManager() { }

        protected override bool addChild(WorkStationViewModel t)
        {
            var vm = _datas.FirstOrDefault(m => m.StaticData.DbId == t.StaticData.DbId);
            if (vm == null)
            {
                _datas.Add(t);
                t.PropertyChanged += T_PropertyChanged;
                OnPropertyChanged("Count");
                return true;
            }
            return false;
        }

        protected override bool removeChild(WorkStationViewModel t)
        {
            var vm = _datas.FirstOrDefault(m => m.StaticData.DbId == t.StaticData.DbId);
            if (vm != null)
            {
                vm.RemoveGraphics();
                _datas.Remove(vm);
                t.PropertyChanged += T_PropertyChanged;
                OnPropertyChanged("Count");
                return true;
            }
            return false;
        }

        private void T_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName.CompareTo("IsOnline") == 0)
            {
                OnPropertyChanged("OnlineCount");
            }
        }

        public override WorkStationViewModel Get(string dbId)
        {
            return _datas.FirstOrDefault(m => m.StaticData.DbId == dbId);
        }
        public WorkStationViewModel GetById(string workStationId)
        {
            return _datas.FirstOrDefault(m => m.StaticData.WorkStationId == workStationId);
        }
    }
}
